package com.dao.impl;

import com.dao.VoucherDao;
import com.pojo.QueryVoucher;
import com.pojo.ShowVoucher;
import com.entry.Voucher;
import com.util.DBHelper;
import com.pojo.MyTicketDetail;
import com.util.PageHelper;

import java.util.ArrayList;

public class VoucherDaoImpl implements VoucherDao {
    private DBHelper db = new DBHelper();

    @Override
    public ArrayList<ShowVoucher> selectQuery(QueryVoucher queryVoucher, PageHelper pageHelper) {
        StringBuilder sql = new StringBuilder("select v.voucher_id, v.no, v.amount, " +
                "e.name as ename, i.name as iname, v.open_time, v.expiry_time, v.uplink_address, " +
                "v.source, v.status, v.payment_interest_type " +
                "from voucher v inner join enterprise e on v.enterprise_id=e.enterprise_id  " +
                "inner join instituty i on v.instituty_id=i.instituty_id where 1=1 ");
        ArrayList list = new ArrayList();
        if (queryVoucher.getNo() != null) {
            sql.append("and no like ? ");
            list.add("%" + queryVoucher.getNo() + "%");
        }
        if (queryVoucher.getAcquiringEnterprise() != null) {
            sql.append(" and v.enterprise_id in (select e.enterprise_id from enterprise where e.name=? ) ");
            list.add(queryVoucher.getAcquiringEnterprise());
        }
        if (queryVoucher.getOpenEnterprise() != null) {
            sql.append("and e.name=?");
            list.add(queryVoucher.getOpenEnterprise());
        }
        if (queryVoucher.getOpenTime() != null) {
            sql.append("and open_time=? ");
            list.add(queryVoucher.getOpenTime());
        }
        if (queryVoucher.getMinAmount() != 0) {
            sql.append("and amount>=? ");
            list.add(queryVoucher.getMinAmount());
        }
        if (queryVoucher.getMaxAmount() != 0) {
            sql.append("and amount<=? ");
            list.add(queryVoucher.getMaxAmount());
        }
        sql.append(" limit ?,?");
        int startIndex=(pageHelper.getIndexPage()-1)*pageHelper.getShowData();
        list.add(startIndex);
        list.add(pageHelper.getShowData());

        return db.selectinfo(sql.toString(), ShowVoucher.class, list.toArray());
    }

    @Override
    public MyTicketDetail selectByVoucher_id(int voucher_id) {
        String sql = "SELECT\n" +
                "voucher.`no`,\n" +
                "voucher.source,\n" +
                "enterprise.`name`,\n" +
                "enterprise.social_uniform_code,\n" +
                "voucher.amount,\n" +
                "voucher.open_time,\n" +
                "voucher.expiry_time,\n" +
                "voucher.payment_interest_type,\n" +
                "voucher.enterprise_id,\n" +
                "enterprise.enterprise_id,\n" +
                "voucher.instituty_id,\n" +
                "instituty.instituty_id,\n" +
                "instituty.`name`,\n" +
                "financing.remark,\n" +
                "financing.instituty_id\n" +
                "FROM\n" +
                "enterprise\n" +
                "INNER JOIN voucher ON voucher.enterprise_id = enterprise.enterprise_id\n" +
                "INNER JOIN instituty ON instituty.instituty_id = voucher.instituty_id\n" +
                "INNER JOIN financing ON financing.instituty_id = instituty.instituty_id\n" +
                "WHERE\n" +
                "voucher.voucher_id = ?";
        return (MyTicketDetail) (db.selectinfo(sql, MyTicketDetail.class, voucher_id).get(0));

    }

    @Override
    public boolean insertVoucher(Voucher voucher) {
        String sql = "insert into voucher values(null,?,?,?,?,?,?,?,?,?,?,?,?)";
        return db.update(sql, voucher.getNo(), voucher.getAmount(), voucher.getEnterprise_id(), voucher.getOpen_enterprise_id(),
                voucher.getInstituty_id(), voucher.getOpen_time(), voucher.getExpiry_time(), voucher.getUplink_address(), voucher.getSource(),
                voucher.getStatus(), voucher.getSource_id(), voucher.getPayment_interest_type()) != 0 ? true : false;
    }

    @Override
    public int selectVoucherSize(QueryVoucher queryVoucher) {
        StringBuilder sql =new StringBuilder("select count(voucher_id) from voucher where 1=1 ");
        ArrayList list = new ArrayList();
        if (queryVoucher.getNo() != null) {
            sql.append("and no like ? ");
            list.add("%" + queryVoucher.getNo() + "%");
        }
        if (queryVoucher.getAcquiringEnterprise() != null) {
            sql.append(" and v.enterprise_id in (select e.enterprise_id from enterprise where e.name=? ) ");
            list.add(queryVoucher.getAcquiringEnterprise());
        }
        if (queryVoucher.getOpenEnterprise() != null) {
            sql.append("and v.open_enterprise_id in (select e.enterprise_id from enterprise where e.name=? ) ");
            list.add(queryVoucher.getOpenEnterprise());
        }
        if (queryVoucher.getOpenTime() != null) {
            sql.append("and open_time=? ");
            list.add(queryVoucher.getOpenTime());
        }
        if (queryVoucher.getMinAmount() != 0) {
            sql.append("and amount>=? ");
            list.add(queryVoucher.getMinAmount());
        }
        if (queryVoucher.getMaxAmount() != 0) {
            sql.append("and amount<=? ");
            list.add(queryVoucher.getMaxAmount());
        }
        return db.selectintfoCount(sql.toString(), list.toArray());
    }

//    @Override
//    public MyTicketFinancingVoucherDetail selectByVoucherid(int Voucher_id) {
//        String sql = "SELECT\n" +
//                "voucher.voucher_id,\n" +
//                "instituty.`name`,\n" +
//                "voucher.open_time,\n" +
//                "voucher.payment_interest_type,\n" +
//                "voucher.amount,\n" +
//                "voucher.expiry_time,\n" +
//                "financing.remark,\n" +
//                "enterprise.social_uniform_code\n" +
//                "FROM\n" +
//                "voucher\n" +
//                "INNER JOIN instituty ON voucher.instituty_id = instituty.instituty_id\n" +
//                "INNER JOIN financing ON voucher.enterprise_id = financing.enterprise_id\n" +
//                "INNER JOIN enterprise ON voucher.enterprise_id = enterprise.enterprise_id\n" +
//                "where voucher_id=?";
//        return (MyTicketFinancingVoucherDetail) (db.selectinfo(sql, MyTicketFinancingVoucherDetail.class, Voucher_id).get(0));
//    }
}
